package com.wechat.page.contact.card;

import com.swak.fx.support.AbstractComp;
import com.swak.fx.support.AbstractPage;
import com.swak.fx.support.Display;
import com.swak.fx.support.FXMLView;
import com.swak.fx.support.ui.UIImage;
import com.swak.fx.support.ui.UITextFlow;
import com.swak.utils.StringUtils;
import com.wechat.dto.ContactDTO;
import com.wechat.page.comps.TextComp;

import javafx.beans.property.LongProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleLongProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.fxml.FXML;

/**
 * 联系人 - 信息
 * 
 * @author 618lf
 */
@FXMLView(title = "联系人 - 信息", value = "ContactInfoComp.fxml", css = "ContactInfoComp.css")
public class ContactInfoComp extends AbstractComp implements TextComp {

	@FXML
	protected UIImage image;
	@FXML
	protected UITextFlow name;
	@FXML
	protected UITextFlow nickName;
	@FXML
	protected UITextFlow office;

	public ContactInfoComp(AbstractPage parent) {
		super(parent);
	}

	@Override
	public void initializeParts() {
		super.initializeParts();
		this.image.image(Display.logo());
	}

	@Override
	public void setupEventHandlers() {
		super.setupEventHandlers();
	}

	@Override
	public void setupValueChangedListeners() {
		super.setupValueChangedListeners();
		this.contactProperty().addListener((ob, o, n) -> this.onContactChange(n));
		this.refreshProperty().addListener((ob, o, n) -> this.onRefresh(n.longValue()));
	}

	private void onRefresh(Long time) {
		ContactDTO contact = this.contactProperty.get();
		if (contact != null) {
			this.onContactChange(contact);
		}
	}

	private void onContactChange(ContactDTO contact) {
		this.buildTextContent(this.name, StringUtils.defaultIfBlank(contact.getAlias(), contact.getNickName()));
		this.buildTextContent(this.nickName, "昵称：" + contact.getNickName());
		this.buildTextContent(this.office, "组织：" + StringUtils.defaultIfBlank(contact.getOffice(), "/"));
	}

	@Override
	public void destory() {
		super.destory();
	}

	// ================================================================================
	// Properties
	// ================================================================================
	private final ObjectProperty<ContactDTO> contactProperty = new SimpleObjectProperty<>();
	private final LongProperty refreshProperty = new SimpleLongProperty();

	public ObjectProperty<ContactDTO> contactProperty() {
		return contactProperty;
	}

	public LongProperty refreshProperty() {
		return refreshProperty;
	}
}